package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        //1.创建一个条件构造器
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //查看是否是当前用户的购物车
        lqw.eq(ShoppingCart::getUserId, BaseContext.getThreadLocal());
        //2.查看数据库里的数据
        List<ShoppingCart> list = shoppingCartService.list(lqw);

        return R.success(list);
    }

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        //1.先拿到用户的id
        Long currentId = BaseContext.getThreadLocal();
        //2.将用户的id放进购物车中
        shoppingCart.setUserId(currentId);
        //3.创建条件构造器,指定是哪个用户的购物车数据
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //相等,拿到当前用户购物车的数据
        lqw.eq(ShoppingCart::getUserId, currentId);
        //4.获取用户的菜品id,判断用户添加的是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        if (dishId != null) {
            //说明添加的是菜品
            lqw.eq(ShoppingCart::getDishId, dishId);
        } else {
            //说明添加的是套餐
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //5.执行方法查看用户购物车的数据
        ShoppingCart cart = shoppingCartService.getOne(lqw);

        //6.判断购物车是否有数据
        if (cart != null) {
            //如果不为空,说明购物车已有数据了,数量加1
            Integer number = cart.getNumber();
            cart.setNumber(number + 1);
            //更新购物车数据
            shoppingCartService.updateById(cart);
        } else {
            //为空说明用户没有数据
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //将用户选择的菜品或套餐保存
            shoppingCartService.save(shoppingCart);
            cart = shoppingCart;
        }


        return R.success(cart);
    }

    @DeleteMapping("/clean")
    public R<String> clean() {
        //1.创建一个条件构造器
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //判断是否是当前用户
        lqw.eq(ShoppingCart::getUserId, BaseContext.getThreadLocal());
        //2.执行删除方法
        shoppingCartService.remove(lqw);

        return R.success("清空购物车成功");
    }

    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        //创建一个条件构造器
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //拿到当前用户的购物车数据
        lqw.eq(ShoppingCart::getUserId, BaseContext.getThreadLocal());

        //判断一下用户减的是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        if (dishId != null) {
            //说明拿的菜品
            lqw.eq(ShoppingCart::getDishId, dishId);
        } else {
            //说明拿的是套餐
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        //执行方法查询一下当前用户的购物车数据
        ShoppingCart cart = shoppingCartService.getOne(lqw);
        if (cart != null) {
            //获取套餐或菜品的数量
            Integer number = cart.getNumber();
            if (number > 1) {
                //减一
                cart.setNumber(number - 1);
                //更新数据
                shoppingCartService.updateById(cart);

            }else {
                shoppingCartService.remove(lqw);
            }




        }


        return R.success(cart);
    }


}
